program fposTerminal;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, Sqlite3DS, fpguiTerm, fpg_base, fpg_main, fpg_salelistbox,
  fposUtils, fposclientcore, fposclientdb, fposdialogs, StarSerial,
  fpg_resources, fpstructs, tcpbuffers, IniFiles;

function CheckTerminalInfo: Boolean;
var
  DB: TSqlite3Dataset;
  RegForm: TRegisterClientForm;
  basefont: String;
  basefontsize: LongInt;
begin
  Result := false;
  CurrencyString := Settings.ReadString('Options', 'CurrencySymbol','');
  CurrencyFormatString := '0.00';
  DB := ClientDB.GetTerminalInfo;
  basefont := Settings.ReadString('Terminal', 'Base font', FPG_DEFAULT_SANS);
  basefontsize := Settings.ReadInteger('Terminal', 'Base font size', 12);
  fpgStyle.DefaultFont := fpgGetFont( basefont + '-' + IntToStr(basefontsize) + ':antialias=true' );
  fpgSetNamedFont('Headline', basefont + '-' + IntToStr(basefontsize+8) + ':antialias=true');
  fpgSetNamedFont('LargeText', basefont + '-' + IntToStr(basefontsize+6) + ':antialias=true');
  fpgSetNamedFont('Label1', basefont + '-' + IntToStr(basefontsize) + ':antialias=true');
  fpgSetNamedFont('StatusLabel', basefont + '-' + IntToStr(basefontsize-2) + ':antialias=true');
  fpgSetNamedFont('List', basefont + '-' + IntToStr(basefontsize+2) + ':antialias=true');
  fpgSetNamedFont('Grid', basefont + '-' + IntToStr(basefontsize) + ':antialias=true');
  if DB.FieldByName('TerminalID').AsInteger<0 then   // Terminal not registered with server
     try
       RegForm := TRegisterClientForm.Create(nil);
       RegForm.ServerPortEdit.Text := IntToStr(DEFAULT_PORT);
       fpgApplication.MainForm := RegForm;
       Result := RegForm.ShowModal=mrOK;
       if Result then
         begin
           ClientDB.GetTerminalInfo;
           fposClient.LogonToServer(RegForm.ServerAddressEdit.Text, StrToInt(RegForm.ServerPortEdit.Text));
         end;
     finally
       RegForm.Free;
     end
  else
    try
      try
        fposClient.LogonToServer(DB.FieldByName('serveraddress').AsString, DB.FieldByName('serverport').AsInteger);
      except
        {$IFDEF DEBUG}
        writeln('Logon failed');
        {$ENDIF}
      end;
    finally
      Result := true;
    end;
end;

procedure MainProc;
begin
  fpgApplication.Initialize;
  {$I ../fposCommon/logo.rc}
  Settings := TIniFile.Create(GetFirePOSConfigDir + 'fpterminal.ini');
  {$IFDEF DEBUG}
  writeln(GetFirePOSConfigDir + 'fpterminal.ini');
  {$ENDIF}
  try
    fposClient := TfposClient.Create(nil);
    ClientDB := TClientDB.Create(GetFirePOSConfigDir);
    if CheckTerminalInfo then
      begin
        ClientDB.OnConcludeSale := @fposClient.SendSaleDetails;
        ClientDB.GetDepartments;
        fposTerm := TfposTerm.Create(fpgApplication);
        FindItemForm := TFindItemForm.Create(fpgApplication);
        ManualEntryForm := TManualEntryForm.Create(fpgApplication);
        SideBar := TSideBar.Create(fpgApplication);
        SetQuantityForm := TSetQuantityForm.Create(fpgApplication);

        fpgApplication.MainForm := fposTerm;
        fposTerm.Show;
        if ClientDB.StaffLogon
          then fpgApplication.Run;
      end;
  finally
    fposTerm.Free;
    ClientDB.Free;
    Settings.Free;
  end;
end;


{$R *.res}

begin
  MainProc;
end.


